﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DeThiTracNghiem
{
    public partial class formChuDe : Form
    {
        CauHoiTracNghiem_LinQDataContext data = new CauHoiTracNghiem_LinQDataContext();

        public formChuDe()
        {
            InitializeComponent();
        }

        private void formChuDe_Load(object sender, EventArgs e)
        {
            textBoxTen.Clear();
            LoadChuDe();
        }

        private void LoadChuDe()
        {
            var cd = data.ChuDes.Select(c => c);
            listBoxChuDe.DataSource = cd;
            listBoxChuDe.DisplayMember = "TenChuDe";
            listBoxChuDe.ValueMember = "MaChuDe";
            listBoxChuDe.SelectedIndex = -1;

        }

        private void buttonThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void listBoxChuDe_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxChuDe.Items.Count > 0)
            {
                textBoxTen.Text = listBoxChuDe.Text;
            }
        }

        private void buttonThem_Click(object sender, EventArgs e)
        {
            if (textBoxTen.Text != "")
            {
                ChuDe cd = new ChuDe();
                cd.TenChuDe = textBoxTen.Text;
                data.ChuDes.InsertOnSubmit(cd);
                data.SubmitChanges();
                formChuDe_Load(sender, e);
                
            }
        }

        private void buttonCapNhat_Click(object sender, EventArgs e)
        {
            if (listBoxChuDe.SelectedIndex > -1)
            {
                int machude = int.Parse(listBoxChuDe.SelectedValue.ToString());
                var cd = data.ChuDes.Single(c => c.MaChuDe == machude);
                cd.TenChuDe = textBoxTen.Text;
                data.SubmitChanges();
                formChuDe_Load(sender, e);
            }
        }

    }
}
